<template>
	<view class="contain">
		<view class="title">
			开设新课
		</view>
		<view class="list">
			<u--form labelPosition="top" :model="model" labelWidth="auto">
				<u-form-item label="名称:" prop="model.name" borderBottom ref="item1">
					<u--input v-model="model.name" border="2px" placeholder="设置课程名称"></u--input>
				</u-form-item>
				<u-form-item label="学分:" prop="model.credit" borderBottom ref="item1">
					<u-number-box v-model="model.credit" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="类型:" prop="model.category" borderBottom @click="showSelect = true; hideKeyboard()"
					ref="item1">
					<u--input v-model="model.category" disabled disabledColor="#ffffff" placeholder="请选择课类型"
						border="none">
					</u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-action-sheet :show="showSelect" :actions="actions" title="请选择课类型" description=""
					@close="showSelect = false" @select="Select">
				</u-action-sheet>

				<u-form-item label="考查:" prop="model.assessment" borderBottom
					@click="showSelect2 = true; hideKeyboard()" ref="item1">
					<u--input v-model="model.assessment" disabled disabledColor="#ffffff" placeholder="请选考查方式"
						border="none">
					</u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-action-sheet :show="showSelect2" :actions="actions2" title="请选考查方式" description=""
					@close="showSelect2 = false" @select="Select2">
				</u-action-sheet>

				<u-form-item label="总学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.total_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="讲授学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.lecture_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="实验学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.experimental_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="实践学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.practical_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="其他学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.other_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-form-item label="周学时:" prop="model.category" borderBottom ref="item1">
					<u-number-box v-model="model.weekly_hours" :step="0.5" @change="" :min="0.0"></u-number-box>
				</u-form-item>
				<u-button class="btn" type="error" @click="submit">提交</u-button>
			</u--form>
		</view>
	</view>
</template>

<script>
	import api from '@/api/index.js'; // 引入api

	export default {
		data() {
			return {
				model: {
					name: "",
					credit: 0,
					category: "",
					assessment: "",
					total_hours: 0.0,
					lecture_hours: 0.0,
					experimental_hours: 0.0,
					practical_hours: 0.0,
					other_hours: 0.0,
					weekly_hours: 0.0,
				},
				actions: [{
						name: '通识教育/必修课',
					},
					{
						name: '专业必修/基础课',
					},
					{

						name: '专业必修/核心课',
					},
					{
						name: '专业选修/限选课',
					},
					{
						name: '专业选修/任选课',
					},
					{
						name: '其他',
					},
				],
				actions2: [{
						name: "考试"
					},
					{
						name: "考查"
					}
				],
				showSelect: false,
				showSelect2: false
			};
		},
		methods: {
			hideKeyboard() {
				uni.hideKeyboard()
			},
			Select(e) {
				this.model.category = e.name
			},
			Select2(e) {
				this.model.assessment = e.name
			},
			submit() {
				// console.log(this.model)
				let token = uni.getStorageSync("token")
				if (this.model.total_hours == (this.model.lecture_hours + this.model.experimental_hours + this.model
						.practical_hours + this.model.other_hours)) {
					api.addCourse(token, this.model).then((res) => {
						if (res.code === 1) {
							uni.showModal({
								content: "添加成功",
								showCancel:false,
								success() {
									uni.switchTab({
										url: "./home"
									})
								}
							})
							
							
						} else if (res.code === 0) {
							uni.showModal({
								content: res.errors
							})
						}
					}).catch((err) => {
						console.log(err)
					})
				} else {
					uni.showModal({
						content: "学时相加不等于总学时",
						showCancel: false
					})
				}

			}
		}
	}
</script>

<style lang="scss">
	.contain {
		.title {
			height: 100px;
			line-height: 100px;
			background-color: #a62618;
			text-align: center;
			color: white;
			font-size: 25px;
		}

		.list {
			// background-color: $u-primary-light;
			margin: 10px;
			
		}
		
	}
</style>
